"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.MentionsOption = void 0;

var _vue = require("vue");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classNames2 = _interopRequireDefault(require("../_util/classNames"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _vcMentions = _interopRequireWildcard(require("../vc-mentions"));

var _mentionsProps = require("../vc-mentions/src/mentionsProps");

var _useConfigInject2 = _interopRequireDefault(require("../_util/hooks/useConfigInject"));

var _propsUtil = require("../_util/props-util");

var _FormItemContext = require("../form/FormItemContext");

var _omit = _interopRequireDefault(require("../_util/omit"));

var _Option = require("../vc-mentions/src/Option");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var getMentions = function getMentions() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var config = arguments.length > 1 ? arguments[1] : undefined;

  var _ref = config || {},
      _ref$prefix = _ref.prefix,
      prefix = _ref$prefix === void 0 ? '@' : _ref$prefix,
      _ref$split = _ref.split,
      split = _ref$split === void 0 ? ' ' : _ref$split;

  var prefixList = Array.isArray(prefix) ? prefix : [prefix];
  return value.split(split).map(function () {
    var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var hitPrefix = null;
    prefixList.some(function (prefixStr) {
      var startStr = str.slice(0, prefixStr.length);

      if (startStr === prefixStr) {
        hitPrefix = prefixStr;
        return true;
      }

      return false;
    });

    if (hitPrefix !== null) {
      return {
        prefix: hitPrefix,
        value: str.slice(hitPrefix.length)
      };
    }

    return null;
  }).filter(function (entity) {
    return !!entity && !!entity.value;
  });
};

var mentionsProps = (0, _extends2.default)((0, _extends2.default)({}, _mentionsProps.mentionsProps), {
  loading: _vueTypes.default.looseBool,
  onFocus: {
    type: Function
  },
  onBlur: {
    type: Function
  },
  onSelect: {
    type: Function
  },
  onChange: {
    type: Function
  },
  notFoundContent: _vueTypes.default.any,
  defaultValue: String,
  id: String
});
var Mentions = (0, _vue.defineComponent)({
  name: 'SMentions',
  inheritAttrs: false,
  props: mentionsProps,
  emits: ['update:value', 'change', 'focus', 'blur', 'select', 'pressenter'],
  slots: ['notFoundContent', 'option'],
  setup: function setup(props, _ref2) {
    var slots = _ref2.slots,
        emit = _ref2.emit,
        attrs = _ref2.attrs,
        expose = _ref2.expose;

    var _a, _b;

    var _useConfigInject = (0, _useConfigInject2.default)('mentions', props),
        prefixCls = _useConfigInject.prefixCls,
        renderEmpty = _useConfigInject.renderEmpty,
        direction = _useConfigInject.direction;

    var focused = (0, _vue.ref)(false);
    var vcMentions = (0, _vue.ref)(null);
    var value = (0, _vue.ref)((_b = (_a = props.value) !== null && _a !== void 0 ? _a : props.defaultValue) !== null && _b !== void 0 ? _b : '');
    var formItemContext = (0, _FormItemContext.useInjectFormItemContext)();
    (0, _vue.watch)(function () {
      return props.value;
    }, function (val) {
      value.value = val;
    });

    var handleFocus = function handleFocus(e) {
      focused.value = true;
      emit('focus', e);
    };

    var handleBlur = function handleBlur(e) {
      focused.value = false;
      emit('blur', e);
      formItemContext.onFieldBlur();
    };

    var handleSelect = function handleSelect() {
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      emit.apply(void 0, ['select'].concat(args));
      focused.value = true;
    };

    var handleChange = function handleChange(val) {
      if (props.value === undefined) {
        value.value = val;
      }

      emit('update:value', val);
      emit('change', val);
      formItemContext.onFieldChange();
    };

    var getNotFoundContent = function getNotFoundContent() {
      var notFoundContent = props.notFoundContent;

      if (notFoundContent !== undefined) {
        return notFoundContent;
      }

      if (slots.notFoundContent) {
        return slots.notFoundContent();
      }

      return renderEmpty.value('Select');
    };

    var getOptions = function getOptions() {
      var _a;

      return (0, _propsUtil.flattenChildren)(((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)) || []).map(function (item) {
        var _a, _b;

        return (0, _extends2.default)((0, _extends2.default)({}, (0, _propsUtil.getOptionProps)(item)), {
          label: (_b = (_a = item.children) === null || _a === void 0 ? void 0 : _a.default) === null || _b === void 0 ? void 0 : _b.call(_a)
        });
      });
    };

    var focus = function focus() {
      vcMentions.value.focus();
    };

    var blur = function blur() {
      vcMentions.value.blur();
    };

    expose({
      focus: focus,
      blur: blur
    });
    (0, _vue.onMounted)(function () {
      (0, _vue.nextTick)(function () {
        if (process.env.NODE_ENV === 'test') {
          if (props.autofocus) {
            focus();
          }
        }
      });
    });
    return function () {
      var _classNames;

      var disabled = props.disabled,
          getPopupContainer = props.getPopupContainer,
          _props$rows = props.rows,
          rows = _props$rows === void 0 ? 1 : _props$rows,
          _props$id = props.id,
          id = _props$id === void 0 ? formItemContext.id.value : _props$id,
          restProps = __rest(props, ["disabled", "getPopupContainer", "rows", "id"]);

      var className = attrs.class,
          otherAttrs = __rest(attrs, ["class"]);

      var otherProps = (0, _omit.default)(restProps, ['defaultValue', 'onUpdate:value', 'prefixCls']);
      var mergedClassName = (0, _classNames2.default)(className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-disabled"), disabled), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-focused"), focused.value), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-rtl"), direction.value === 'rtl'), _classNames));
      var mentionsProps = (0, _extends2.default)((0, _extends2.default)((0, _extends2.default)((0, _extends2.default)({
        prefixCls: prefixCls.value
      }, otherProps), {
        disabled: disabled,
        direction: direction.value,
        filterOption: props.filterOption,
        getPopupContainer: getPopupContainer,
        options: props.options || getOptions(),
        class: mergedClassName
      }), otherAttrs), {
        rows: rows,
        onChange: handleChange,
        onSelect: handleSelect,
        onFocus: handleFocus,
        onBlur: handleBlur,
        ref: vcMentions,
        value: value.value,
        id: id
      });
      return (0, _vue.createVNode)(_vcMentions.default, mentionsProps, {
        notFoundContent: getNotFoundContent,
        option: slots.option
      });
    };
  }
});
/* istanbul ignore next */

var MentionsOption = (0, _vue.defineComponent)((0, _extends2.default)((0, _extends2.default)({}, _vcMentions.Option), {
  name: 'SMentionsOption',
  props: _Option.optionProps
}));
exports.MentionsOption = MentionsOption;

var _default = (0, _extends2.default)(Mentions, {
  Option: MentionsOption,
  getMentions: getMentions,
  install: function install(app) {
    app.component(Mentions.name, Mentions);
    app.component(MentionsOption.name, MentionsOption);
    return app;
  }
});

exports.default = _default;